import java.util.Arrays;

/**
 * @Author: ColorSheep
 * @Date: 28 6月 2025 10:27
 * @Version: 1.0
 * @Description: TODO
 */
public class MyArrayList<E> implements MyList<E> {
    
    private static final int DEFAULT_CAPACITY = 10; // 默认容量, 没有指定容量情况下的默认容量
    
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
    transient Object[] elementData; // 存储数据的数组
    
    private int size; // 当前元素个数
    
    public MyArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    
    
    @Override
    public boolean add(E e) {
        int minCapacity = size + 1;
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            // 没有指定容量, 默认容量为10, 指定容量如果大于10, 则使用指定容量
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        // 判断是否需要扩容: 能容纳下当前元素的最小容量大于了当前数组长度, 那么这个数组就要进行扩容了
        if (minCapacity - elementData.length > 0) {
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0) {
                newCapacity = minCapacity; // 处理极端情况（如一次性addAll大量元素）
            }
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
        elementData[size++] = e;
        return true;
    }
    
    @Override
    public E get(int index) {
        return (E) elementData[index];
    }
    
    @Override
    public boolean remove(int index) {
        int numMoved = size - index - 1;
        if (numMoved > 0) {
            // 从原始数组的某个位置，拷贝到目标对象的某个位置开始后n个元素
            System.arraycopy(elementData, index + 1, elementData, index, numMoved);
        }
        elementData[--size] = null; // clear to let GC do its work
        return true;
    }
    
    public E pop(int index){
        E oldValue = (E) elementData[index];
        int numMoved = size - index - 1;
        if (numMoved > 0) {
            // 从原始数组的某个位置，拷贝到目标对象的某个位置开始后n个元素
            System.arraycopy(elementData, index + 1, elementData, index, numMoved);
        }
        elementData[--size] = null; // clear to let GC do its work
        return oldValue;
    }
    
    @Override
    public String toString() {
        return "ArrayList{" +
                "elementData=" + Arrays.toString(elementData) +
                ", size=" + size +
                '}';
    }
    
    @Override
    public boolean addFirst(E e) {
        return false;
    }
    
    @Override
    public boolean addLast(E e) {
        return false;
    }
    
  
    
    @Override
    public E getFirst() {
        return null;
    }
    
    @Override
    public E getLast() {
        return null;
    }
    

    
    
    @Override
    public boolean remove(E e) {
        return false;
    }
    
    @Override
    public boolean removeFirst() {
        return false;
    }
    
    @Override
    public boolean removeLast() {
        return false;
    }
    
    @Override
    public int size() {
        return size;
    }
    
    @Override
    public boolean contains(E e) {
        return false;
    }
    
    @Override
    public void printList() {
    
    }
}
